package com.kenans.contorller;

import com.github.pagehelper.Page;
import com.kenans.common.JsonResult;
import com.kenans.dao.UserDao;
import com.kenans.entity.Order;
import com.kenans.entity.User;
import com.kenans.service.OrderService;
import com.kenans.service.UserService;
import com.kenans.vo.VOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author KENANS
 * @date 2020-8-27 16:37
 * @Version 1.0
 */
@Controller
@RequestMapping("/order")
public class OrderContorller {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserDao userDao;

    /**
     * ?getid=7&backid=11&cid=6 还有一个total 订车的总价格
     * 这是一个提交订单信息的页面
     * @return
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public JsonResult add(Integer getid, Integer backid, Integer cid, Double toatal, HttpSession session) {
        User user = (User)session.getAttribute("loginUser");
        User login = userDao.findByTel(user.getTel());
        Order order = new Order(cid, login.getId(), getid, backid, toatal, "已预订");
        orderService.addOrder(order);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(1);
        jsonResult.setInfo("预订成功");
        return jsonResult;
    }

    @RequestMapping("/all.do")
    @ResponseBody
    public JsonResult findAll(HttpSession session, Integer page) {
        User user = (User)session.getAttribute("loginUser");

        Map<String, Object> info = orderService.findOrder(user.getId(), page);

        // 前端要一个total 和obj obj 应该是一个list
        JsonResult jsonResult = new JsonResult();
        jsonResult.setInfo(info);
        jsonResult.setCode(1);
        return jsonResult;
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public JsonResult deleteOrder(Integer id) {
        orderService.delete(id);
        return new JsonResult(1, "删除成功");
    }

}
